package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.PlatformEnumForPublish;
import com.jilefojing.beans.enums.ProgressStatusForSource;
import com.jilefojing.beans.enums.StatusEnumForPublish;
import com.jilefojing.beans.pojos.Recommend;
import com.jilefojing.beans.query.RecommendQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IRecommendDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IRecommendMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月19日 17时57分48秒
 */
@Repository
public class RecommendDao extends BaseDao<Recommend> implements IRecommendDao {
	@Autowired
	private IRecommendMapper recommendMapper;

	@Override
	public Pagination<Recommend> getPagination(RecommendQuery recommendQuery, Pagination<Recommend> pagination) {
		Example example = new Example(Recommend.class);
		Criteria criteria = example.createCriteria();

		if (Detect.notEmpty(recommendQuery.getIds())) {
			criteria.andIn(Recommend.FIELD_ID, recommendQuery.getIds());
		}

		if (Detect.notEmpty(recommendQuery.getItemTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _TypeEnum : recommendQuery.getItemTypes()) {
				itemTypeCodes.add(_TypeEnum.getCode());
			}
			criteria.andIn(Recommend.FIELD_ITEMTYPE, itemTypeCodes);
		}

		if (Detect.notEmpty(recommendQuery.getItemIds())) {
			criteria.andIn(Recommend.FIELD_ITEMID, recommendQuery.getItemIds());
		}

		if (Detect.notEmpty(recommendQuery.getStatuses())) {
			List<Short> statusCodes = new ArrayList<Short>();
			for (StatusEnumForPublish _Enum : recommendQuery.getStatuses()) {
				statusCodes.add(_Enum.getCode());
			}
			criteria.andIn(Recommend.FIELD_STATUS, statusCodes);
		}

		if (Detect.notEmpty(recommendQuery.getProgressStatuses())) {
			List<Short> statusCodes = new ArrayList<Short>();
			for (ProgressStatusForSource _Enum : recommendQuery.getProgressStatuses()) {
				statusCodes.add(_Enum.getCode());
			}
			criteria.andIn(Recommend.FIELD_PROGRESSSTATUS, statusCodes);
		}

		if (Detect.notEmpty(recommendQuery.getPlatforms())) {
			List<Short> plateformCodes = new ArrayList<Short>();
			for (PlatformEnumForPublish _Enum : recommendQuery.getPlatforms()) {
				plateformCodes.add(_Enum.getCode());
			}
			criteria.andIn(Recommend.FIELD_PLATFORM, plateformCodes);
		}

		if (Detect.notEmpty(recommendQuery.getCreateUserIds())) {
			criteria.andIn(Recommend.FIELD_CREATEUSER, recommendQuery.getCreateUserIds());
		}

		if (recommendQuery.getEqualStartTime() != null) {
			criteria.andEqualTo(Recommend.FIELD_STARTTIME, recommendQuery.getEqualStartTime());
		}

		if (recommendQuery.getEqualAndBeforStartTime() != null) {
			criteria.andGreaterThanOrEqualTo(Recommend.FIELD_STARTTIME, recommendQuery.getEqualAndBeforStartTime());
		}

		if (recommendQuery.getEqualAndAfterStartTime() != null) {
			criteria.andLessThanOrEqualTo(Recommend.FIELD_STARTTIME, recommendQuery.getEqualAndAfterStartTime());
		}

		if (recommendQuery.getBeforStartTime() != null) {
			criteria.andGreaterThan(Recommend.FIELD_STARTTIME, recommendQuery.getBeforStartTime());
		}

		if (recommendQuery.getAfterStartTime() != null) {
			criteria.andLessThan(Recommend.FIELD_STARTTIME, recommendQuery.getAfterStartTime());
		}

		if (recommendQuery.getEqualEndTime() != null) {
			criteria.andEqualTo(Recommend.FIELD_ENDTIME, recommendQuery.getEqualEndTime());
		}

		if (recommendQuery.getEqualAndBeforEndTime() != null) {
			criteria.andGreaterThanOrEqualTo(Recommend.FIELD_ENDTIME, recommendQuery.getEqualAndBeforEndTime());
		}

		if (recommendQuery.getEqualAndAfterEndTime() != null) {
			criteria.andLessThanOrEqualTo(Recommend.FIELD_ENDTIME, recommendQuery.getEqualAndAfterEndTime());
		}

		if (recommendQuery.getBeforEndTime() != null) {
			criteria.andGreaterThan(Recommend.FIELD_ENDTIME, recommendQuery.getBeforEndTime());
		}

		if (recommendQuery.getAfterEndTime() != null) {
			criteria.andLessThan(Recommend.FIELD_ENDTIME, recommendQuery.getAfterEndTime());
		}

		if (recommendQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(Recommend.FIELD_CREATETIME, recommendQuery.getEqualCreateTime());
		}

		if (recommendQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(Recommend.FIELD_CREATETIME, recommendQuery.getEqualAndBeforCreateTime());
		}

		if (recommendQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(Recommend.FIELD_CREATETIME, recommendQuery.getEqualAndAfterCreateTime());
		}

		if (recommendQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(Recommend.FIELD_CREATETIME, recommendQuery.getBeforCreateTime());
		}

		if (recommendQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(Recommend.FIELD_CREATETIME, recommendQuery.getAfterCreateTime());
		}

		if (recommendQuery.getEqualUpdateTime() != null) {
			criteria.andEqualTo(Recommend.FIELD_UPDATETIME, recommendQuery.getEqualUpdateTime());
		}

		if (recommendQuery.getEqualAndBeforUpdateTime() != null) {
			criteria.andGreaterThanOrEqualTo(Recommend.FIELD_UPDATETIME, recommendQuery.getEqualAndBeforUpdateTime());
		}

		if (recommendQuery.getEqualAndAfterUpdateTime() != null) {
			criteria.andLessThanOrEqualTo(Recommend.FIELD_UPDATETIME, recommendQuery.getEqualAndAfterUpdateTime());
		}

		if (recommendQuery.getBeforUpdateTime() != null) {
			criteria.andGreaterThan(Recommend.FIELD_UPDATETIME, recommendQuery.getBeforUpdateTime());
		}

		if (recommendQuery.getAfterUpdateTime() != null) {
			criteria.andLessThan(Recommend.FIELD_UPDATETIME, recommendQuery.getAfterUpdateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}
		return super.getPagination(pagination, example);
	}

	@Override
	public List<Recommend> getListByItem(ItemTypeEnumForSource itemType, long itemId) {
		return this.getListByItems(itemType, Arrays.asList(new Long[] { itemId }));
	}

	@Override
	public List<Recommend> getListByItems(ItemTypeEnumForSource itemType, List<Long> itemIds) {
		Example example = new Example(Recommend.class);
		example.createCriteria().andIn(Recommend.FIELD_ITEMID, itemIds).andEqualTo(Recommend.FIELD_ITEMTYPE, itemType.getCode());
		return this.getListByExample(example);
	}

}
